<!--********************************************************************
* Copyright© 2000 - 2018 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title data-i18n="resources.title_iPortalSecurity"></title>

    <style>
        .panel {
            margin-bottom: 0;
        }
    </style>
</head>
<body style=" margin: 0;overflow: auto;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
<div class="container">
    <div class="page-header">
        <h4 data-i18n="resources.title_iPortalSecurity"></h4>
    </div>
    <div class="row">
        <table class="table table-bordered col-md-6">
            <thead>
            <tr>
                <th class="text-center" data-i18n="resources.text_function"></th>
                <th class="text-center" data-i18n="resources.text_instance"></th>
            </tr>
            </thead>
            <tbody>
            <!--登录-->
            <tr>
                <td class="text-center text-success" data-i18n="resources.text_login"></td>
                <td>
                    <div class="col-md-10 col-md-offset-1">
                        <form class="form-horizontal">
                            <fieldset>
                                <div class="form-group">
                                    <label for="loginUrl_iportal" class="col-md-2 control-label"
                                           data-i18n="resources.text_serviceAddress" style="width:26.666667%;"></label>
                                    <div class="col-md-8">
                                        <input type="text" class="form-control" id="loginUrl_iportal"
                                               data-i18n="[placeholder]resources.text_input_tips_serviceDomainName">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="username_iportal" class="col-md-2 control-label" data-i18n="resources.text_userName" style="width:26.666667%;"></label>

                                    <div class="col-md-8">
                                        <input type="text" class="form-control" id="username_login">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="password_iportal" class="col-md-2 control-label" data-i18n="resources.text_password" style="width:26.666667%;"></label>

                                    <div class="col-md-8">
                                        <input type="password" class="form-control" id="password_login">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-12 text-center">
                                        <a class="btn btn-success" onclick="loginPortal()" data-i18n="resources.text_login"></a>
                                        <a class="btn btn-success" onclick="logoutPortal()" data-i18n="resources.text_logout"></a>
                                    </div>
                                </div>
                            </fieldset>
                        </form>
                    </div>
                </td>
            </tr>

            <!--token 申请-->
            <tr>
                <td class="text-center text-success" data-i18n="resources.text_applyForToken"></td>
                <td>
                    <div class="col-md-10 col-md-offset-1">
                        <form class="form-horizontal">
                            <fieldset>
                                <div class="form-group">
                                    <label for="serviceUrlToken_iportal" class="col-md-2 control-label" data-i18n="resources.text_serviceAddress" style="width:26.666667%;"></label>
                                    <div class="col-md-8">
                                        <input type="text" class="form-control" id="serviceUrlToken_iportal"
                                               data-i18n="[placeholder]resources.text_input_tips_serviceDomainName">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="username_iportal" class="col-md-2 control-label" data-i18n="resources.text_userName" style="width:26.666667%;"></label>

                                    <div class="col-md-8">
                                        <input type="text" class="form-control" id="username_iportal">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="password_iportal" class="col-md-2 control-label" data-i18n="resources.text_password" style="width:26.666667%;"></label>

                                    <div class="col-md-8">
                                        <input type="password" class="form-control" id="password_iportal">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="clientType_iportal" class="col-md-2 control-label" data-i18n="resources.text_clientID" style="width:26.666667%;"></label>

                                    <div class="col-md-8">
                                        <select class="form-control" id="clientType_iportal" name="clientType">
                                            <option value="Referer" selected="selected">HTTP Referer</option>
                                            <option value="IP" data-i18n="resources.text_clientIP"></option>
                                            <option value="RequestIP" data-i18n="resources.text_currentRequestIP"></option>
                                            <option value="NONE" data-i18n="resources.text_noClientLimit"></option>

                                        </select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="referer_iportal" class="col-md-2 control-label" data-i18n="resources.text_refererIPEmpty" style="width:26.666667%;"></label>

                                    <div class="col-md-8">
                                        <input class="form-control" id="referer_iportal">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="expiration_iportal" class="col-md-2 control-label" data-i18n="resources.text_termValid" style="width:26.666667%;"></label>

                                    <div class="col-md-8">
                                        <select class="form-control" id="expiration_iportal" name="expiration">
                                            <option value="60" selected="selected" data-i18n="resources.text_oneHour"></option>
                                            <option value="1440" data-i18n="resources.text_oneDay"></option>
                                            <option value="10080" data-i18n="resources.text_oneWeek"></option>
                                            <option value="43200" data-i18n="resources.text_oneMonth"></option>
                                            <option value="525600" data-i18n="resources.text_oneYear"></option>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-12 text-center">
                                        <a class="btn btn-success" onclick="generatePortalToken()" data-i18n="resources.text_applyToken"></a>
                                    </div>
                                </div>
                            </fieldset>
                        </form>
                    </div>
                </td>
            </tr>
            <!-- 注册token -->
            <tr>
                <td class="text-center text-success" data-i18n="resources.text_tokenAuthorized"></td>
                <td>
                    <div class="col-md-10 col-md-offset-1">
                        <form class="form-horizontal">
                            <fieldset>
                                <div class="form-group">
                                    <label for="tokenServiceUrl_iportal" class="col-md-2 control-label" data-i18n="resources.text_serviceAddress" style="width:26.666667%;"></label>
                                    <div class="col-md-8">
                                        <input type="text" class="form-control" id="tokenServiceUrl_iportal"
                                               placeholder="http://localhost:8090/web/services/32">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="token_iportal" class="col-md-2 control-label" style="width:26.666667%;">Token</label>
                                    <div class="col-md-8">
                                        <input type="text" class="form-control" id="token_iportal">
                                    </div>
                                </div>

                                <div class="col-md-12 text-center">
                                    <a class="btn btn-success" onclick="registerPortalTokenAndRequestService()" data-i18n="resources.text_accessing"></a>
                                </div>
                            </fieldset>
                        </form>
                    </div>
                </td>
            </tr>
            <!-- 注册key -->
            <tr>
                <td class="text-center text-success" data-i18n="resources.text_keyAuthorized"></td>
                <td>
                    <div class="col-md-10 col-md-offset-1">
                        <form class="form-horizontal">
                            <fieldset>
                                <div class="form-group">
                                    <label for="serviceUrl_iportal" class="col-md-2 control-label" data-i18n="resources.text_serviceAddress" style="width:26.666667%;"></label>
                                    <div class="col-md-8">
                                        <input type="text" class="form-control" id="serviceUrl_iportal"
                                               data-i18n="[placeholder]resources.text_iPortalServicesAddress">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="key_iportal" class="col-md-2 control-label" style="width:26.666667%;">Key</label>
                                    <div class="col-md-8">
                                        <input type="text" class="form-control" id="key_iportal">
                                    </div>
                                </div>

                                <div class="col-md-12 text-center">
                                    <a class="btn btn-success" onclick="registerPortalKeyAndRequestService()" data-i18n="resources.text_accessing"></a>
                                </div>
                            </fieldset>
                        </form>
                    </div>
                </td>
            </tr>
            <!-- 出图 -->
            <tr>
                <td class="text-center text-success" data-i18n="resources.text_keyAuthorizedGetMap"></td>
                <td>
                    <div class="col-md-10 col-md-offset-1">
                        <form class="form-horizontal">
                            <fieldset>
                                <div class="form-group">
                                    <label for="serviceUrlMap_iportal" class="col-md-2 control-label" data-i18n="resources.text_serviceAddress" style="width:26.666667%;"></label>
                                    <div class="col-md-8">
                                        <input type="text" class="form-control" id="serviceUrlMap_iportal">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="mapKey_iportal" class="col-md-2 control-label" style="width:26.666667%;">Key</label>
                                    <div class="col-md-8">
                                        <input type="text" class="form-control" id="mapKey_iportal">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-12  text-center">
                                        <a class="btn btn-success" onclick="showMapWithPortalKey()"  data-i18n="resources.text_getMap"></a>
                                    </div>
                                </div>
                            </fieldset>
                        </form>
                    </div>
                </td>
            </tr>
            <tr>
                <td colspan="3">
                    <div class="row">
                        <div class="col-md-12  text-center">
                            <div class="col-md-12" id="iportal_map" style=" height: 300px;"></div>
                        </div>
                    </div>
                </td>
            </tr>
            </tbody>
        </table>
    </div>
</div>
<script type="text/javascript" include="jquery,bootstrap,widgets.alert" src="../js/include-web.js"></script>
<script type="text/javascript" src="../../dist/leaflet/include-leaflet.js"></script>
<script>

    //若示例与SuperMap iPortal不在同一域下，欲查看示例效果请打开该句注释
    //SuperMap.Support.cors = false;
    $(document).ready(function () {
        $("#loginUrl_iportal").val("http://localhost:8090/iportal");
        $("#tokenServiceUrl_iportal").val("http://support.supermap.com.cn:8092/web/services/748");
        $("#token_iportal").val("_Bo_ksR9DC56MvnAXc6liuhAdWmmt1SuoLX758QIeBJUvteLqHqUeeHnQ6OU0i0VBoN5FjNP_SOQ2o1xyudBjA..");

        $("#serviceUrl_iportal").val("http://support.supermap.com.cn:8099/ywutslmu/iserver/services/map-china400/rest/maps/China_4326");
        $('#key_iportal').val("CBIrkfXEAjydLrfp364qgM3P");
        $("#serviceUrlMap_iportal").val("http://support.supermap.com.cn:8099/ywutslmu/iserver/services/map-china400/rest/maps/China_4326");
        $('#mapKey_iportal').val("CBIrkfXEAjydLrfp364qgM3P");

    });

    /*登录*/
    function loginPortal() {
        var loginUrl = $("#loginUrl_iportal").val();
        var userName = $('#username_login').val();
        var password = $('#password_login').val();
        if (!checkUrl(loginUrl)) {
            return;
        }
        SuperMap.SecurityManager.loginiPortal(loginUrl, userName, password).then(function (result) {
            if (result.succeed)
                widgets.alert.showAlert(resources.msg_loginSuccess, true);
        });
    }

    /*登出*/
    function logoutPortal() {
        var loginUrl = $("#loginUrl_iportal").val();
        SuperMap.SecurityManager.logoutiPortal(loginUrl).then(function (succeed) {
            if (succeed) {
                widgets.alert.showAlert(resources.msg_logoutSuccess, true);
            }
        });
    }


    /*登录 end*/

    /*申请token*/
    function generatePortalToken() {
        var serverTokenUrl = $("#serviceUrlToken_iportal").val();
        if (!checkUrl(serverTokenUrl)) {
            return;
        }
        var serverInfo = new SuperMap.ServerInfo(SuperMap.ServerType.IPORTAL, {
            server: serverTokenUrl
        });
        SuperMap.SecurityManager.registerServers([serverInfo]);
        var userName = $('#username_iportal').val();
        var password = $('#password_iportal').val();
        var clientType = $('#clientType_iportal').val();
        var referer = $('#referer_iportal').val();
        var expiration = $('#expiration_iportal').val();
        SuperMap.SecurityManager.generateToken(serverTokenUrl, new SuperMap.TokenServiceParameter({
            userName: userName,
            password: password,
            clientType: clientType,
            referer: referer,
            ip: referer,
            expiration: expiration
        })).then(function (result) {
            widgets.alert.showAlert(result, true);
        });
    }

    /*申请token end*/

    /*注册token*/
    function registerPortalTokenAndRequestService() {
        var key = $('#token_iportal').val();
        var serviceUrl = getPortalTokenServiceUrl();
        if (!checkUrl(serviceUrl)) {
            return;
        }
        SuperMap.SecurityManager.destroyToken(serviceUrl);
        SuperMap.SecurityManager.registerToken(serviceUrl, key);
        var iPortalService = new SuperMap.iPortalService(serviceUrl);
        iPortalService.load().then(function (json) {
            if (json && json.error) {
                widgets.alert.showAlert(JSON.stringify(json.error), false);
            } else {
                widgets.alert.showAlert("proxiedUrl:" + iPortalService.proxiedUrl, true);
            }

        });
    }

    function getPortalTokenServiceUrl() {
        return $("#tokenServiceUrl_iportal").val();
    }

    /*注册token end*/

    /*注册key*/

    function registerPortalKeyAndRequestService() {
        var key = $('#key_iportal').val();
        var serviceUrl = getPortalKeyServiceUrl();
        if (!checkUrl(serviceUrl)) {
            return;
        }
        SuperMap.SecurityManager.destroyKey(serviceUrl);
        SuperMap.SecurityManager.registerKey(serviceUrl, key);
        requestService(serviceUrl);
    }


    function requestService(url) {
        if (!checkUrl(url)) {
            return;
        }
        L.supermap.mapService(url, {
            serverType: SuperMap.ServerType.IPORTAL
        }).getMapInfo(function (json) {
            if (json.error) {
                widgets.alert.showAlert(JSON.stringify(json.error), false);
            } else {
                widgets.alert.showAlert(JSON.stringify(json.result), true);
            }
        });
    }

    function getPortalKeyServiceUrl() {
        return $("#serviceUrl_iportal").val();
    }

    /*注册key end*/



    /*出图*/
    function showMapWithPortalKey() {
        var key = $('#mapKey_iportal').val();
        var mapUrl = $('#serviceUrlMap_iportal').val();
        if (!checkUrl(mapUrl)) {
            return;
        }
        SuperMap.SecurityManager.registerKey(mapUrl, key);

        var onlineMap = L.map('iportal_map', {
            crs: L.CRS.EPSG4326,
            center: [30, 104],
            maxZoom: 18,
            zoom: 2
        });
        L.supermap.tiledMapLayer(mapUrl, {
            serverType: SuperMap.ServerType.IPORTAL
        }).addTo(onlineMap);
    }

    /*出图 end*/

    function checkUrl(url) {
        if (url === "") {
            widgets.alert.showAlert(resources.msg_fillInURL, false);
            return false;
        }
        return true;
    }
</script>
</body>
</html>